const db = require("../config/config");
module.exports = {
 //查询用户信息
  getUerList(req,res) {
    db.dbMysql(`SELECT * FROM user where user_type=3;`, [], function (err, data) {
        if (data) {
            if (data.length > 0) {
              console.log(data);
              res.send({
                code: 200,
                data,
                msg: '查询成功'
              })
            }
        }else {
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
  //查询用户收货地址
  getUserShopAddress(req,res) {
    let address_userid = req.body.user_id
    db.dbMysql(`SELECT * FROM shop_address WHERE address_userid=?;`, [address_userid], function (err, data) {
        if (data) {
            if (data.length > 0) {
              console.log(data);
              res.send({
                code: 200,
                data,
                msg: '查询成功'
              })
            }
        }else {
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
   //新增用户
   addUser(req,res) {
    let user_name = req.body.user_name
    let user_email = req.body.user_emails
    let user_birthday = req.body.user_birthday
    let user_img = req.body.user_img
    let user_pwd = req.body.user_pwd
    let user_sex = req.body.user_sex
    let user_address = req.body.user_address
    let user_phone = req.body.user_phone
    let user_creattime = req.body.user_creattime
    if(user_sex=="男"){
      user_sex=0
    }else{
      user_sex=1
    }
    console.log(req.body);
    db.dbMysql(`INSERT INTO user(user_name,user_email,user_pwd,user_birthday,user_phone,user_creattime,user_sex,user_address,user_img) VALUES (?,?,?,?,?,?,?,?,?);`, [user_name,user_email,user_pwd,user_birthday,user_phone,user_creattime,user_sex,user_address,user_img], function (err, data) {
        if (!err) {
          
              res.send({
                code: 200,
                msg: '添加成功'
              })
        }else {
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
  deleteUser(req,res) {
    let user_id=req.body.user_id
    db.dbMysql(`delete from user where user_id=?;`, [user_id], function (err, data) {
      if (!err) {
            res.send({
              code: 200,
              msg: '删除成功'
            })
            }else {
              res.send({
                code: 500,
                msg: "服务器错误",
              });
            }
        })
  },
  //获取订单信息
  getUserOrder(req,res) {
    let user_id=req.body.user_id
    db.dbMysql(`SELECT * FROM orderinfo where orBuyerId=?;`, [user_id], function (err, data) {
        if (data) {
            if (data.length > 0) {
              console.log(data);
              res.send({
                code: 200,
                data,
                msg: '查询成功'
              })
            }
        }else {
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
  //更改用户信息
  detailUser(req,res) {
    let user_id = req.body.user_id
    let user_name = req.body.user_name
    let user_email = req.body.user_emails
    let user_birthday = req.body.user_birthday
    let user_img = req.body.user_img
    let user_sex = req.body.user_sex
    let user_address = req.body.user_address
    let user_phone = req.body.user_phone
    if(user_sex=="男"){
      user_sex=0
    }else{
      user_sex=1
    }
    db.dbMysql(`UPDATE user set user_name=?,user_email=?,user_birthday=?,user_phone=?,user_sex=?,user_address=?,user_img=? WHERE user_id=?;`, [user_name,user_email,user_birthday,user_phone,user_sex,user_address,user_img,user_id], function (err, data) {
      console.log(user_id);
        if (!err) {
              res.send({
                code: 200,
                msg: '更改成功'
              })
            }else {
              console.log(err);
              res.send({
                code: 500,
                msg: "服务器错误",
              });
            }
          })
  },
  //获取订单信息
  searchUser(req,res) {
    console.log(typeof(req.body.user_creattime));
    let sql=""
    let params = []
    let user_id=req.body.user_id
    let user_name=req.body.user_name
    let user_creattime=req.body.user_creattime
    if(user_id==undefined&&user_name==undefined){
      sql=`SELECT * FROM user where  user_creattime LIKE '%?%';`
      params=[user_creattime]
    }else if(user_creattime==undefined&&user_name==undefined){
      sql=`SELECT * FROM user where  user_id=?;`
      params=[user_id]
    }else if(user_creattime==undefined&&user_id==undefined){
      sql=`SELECT * FROM user where  user_name like concat("%",?,"%");`
      params=[user_name]
    }else if(user_creattime==undefined){
      sql=`SELECT * FROM user where  user_id=? and user_name like concat("%",?,"%");`
      params=[user_id,user_name]
    }else if(user_id==undefined){
      sql=`SELECT * FROM user where  user_creattime LIKE concat("%",?,"%") and user_name like concat("%",?,"%");`
      params=[user_creattime,user_name]
    }else if(user_name==undefined){
      sql=`SELECT * FROM user where  user_creattime LIKE concat("%",?,"%") and user_id=?;`
      params=[user_creattime,user_id]
    }else{
      sql=`SELECT * FROM user where user_creattime LIKE concat("%",?,"%") and user_id=? and user_name like '%?%';`
      params=[user_creattime,user_id,user_name]
    }
    db.dbMysql(sql, params, function (err, data) {
        if (data) {
              console.log(data);
              res.send({
                code: 200,
                data,
                msg: '查询成功'
              })
        }else {
          console.log(err);
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
  //获取用户购买力
  getUserPurchasingPower(req,res) {
    let allData=[]
    db.dbMysql(`SELECT user_id,user_name,user_email from user;`, [], function (err, data) {
        if (data) {
            if (data.length > 0) {
              for(let i=0;i<data.length;i++){
                let user_id=data[i].user_id
                let user_name=data[i].user_name
                let user_email=data[i].user_email
                db.dbMysql(`SELECT sum(orPrice) AS 消费总金额,sum(orNum) AS 消费次数,avg(orPrice) AS 订单均价,max(orStartTime) AS 最近购买日期 FROM orderinfo WHERE orBuyerId =?`, [user_id], function (err, data) {
                    if (data[0].消费总金额!=null) {
                      console.log(user_id,i,allData);
                      allData.push({
                        user_name,
                        user_email,
                        data
                      })
                     return allData
                    }
                    if(i==4){
                      console.log(allData);
                      res.send({
                        code: 200,
                        data:allData,
                        msg: '查询成功'
                      })
                    }

                  })

              }

              
            }
        }else {
            res.send({
              code: 500,
              msg: "服务器错误",
            });
          }
        })
  },
};
